
#include "EnviFilter.h"
#include <qbprotocol/include/SSFilters.h>
#include <uam/UserAccountManager.h>
#include <core/FileSys.h>
#include <QSettings>

CEnviFilter::CEnviFilter()
{
	m_mapBroker.clear();
	m_nFilterType = FilterType::FilterType_NONE;
	m_bIsDispScheme = false;
	m_nChgDisp = -1;
	m_bCorpLoadFlag = true;
	m_bEnableChgDisp = false;
}

CEnviFilter::~CEnviFilter()
{
	SaveEnviFilter();
}

void CEnviFilter::SetFatherKey(const QString& strKey)
{
	if (strKey.isEmpty())
		return;
	m_strFatherKey = strKey;
	m_strSection = QString::fromLocal8Bit(SSUserAccountManager::instance().GetUserInfo().m_UserId)
		+ "-" + strKey;
}

void CEnviFilter::LoadEnviFilter()
{
	if (m_strSection.isEmpty())
		return;

	LoadCorp();

	m_bIsDispScheme = IsSelected("Disp");
	m_strId = GetIniString("CurID");

	QString strTmp;
	if (m_bEnableChgDisp)
	{
		strTmp = GetIniString("ChgDisp").trimmed();
		m_nChgDisp = strTmp.toInt();
		m_nChgDisp=(m_nChgDisp<0 || m_nChgDisp>1?0:m_nChgDisp);
	}

	strTmp = GetIniString("CurType").trimmed();
	if (strTmp.toInt() == FilterType::FilterType_Condition)
	{
		if (IsConditionExist(m_strId))
		{
			m_nFilterType=FilterType::FilterType_Condition;
		}
		else
		{
			m_nFilterType=FilterType::FilterType_NONE;
			m_strId = "";
		}
	}
	else if (strTmp.toInt() == FilterType::FilterType_Bond)
	{
		if (IsGroupExist(m_strId ))
		{
			m_nFilterType=FilterType::FilterType_Bond;
		}
		else
		{
			m_nFilterType=FilterType::FilterType_NONE;
			m_strId = "";
		}
	}
	else
	{
		m_nFilterType=FilterType::FilterType_NONE;
		m_strId = "";
	}
}

void CEnviFilter::LoadCorp()
{
	if (!m_bCorpLoadFlag)
		return;

	m_mapBroker.clear();

	const CBrokerList& brokerlist = SSUserAccountManager::instance().GetReadonlyBroker();
	for (CBrokerList::BrokerMap::const_iterator itr=brokerlist.GetBrokerMap().begin();itr!=brokerlist.GetBrokerMap().end();++itr)
	{
		if (!itr->second.m_enableFlag_BOND && !itr->second.m_enableFlag_NCD)
			continue;
		QString strTmp = QString::number(itr->second.m_brokerID);
		m_mapBroker.insert(std::pair<int,bool>(itr->second.m_brokerID,IsSelected(strTmp)));
	}
}

bool CEnviFilter::IsSelected(const QString &strKey)
{
	QString path = FileSys::instance()->appCfgDir() + "/Filter.ini";
	QSettings setting(path, QSettings::IniFormat);

	setting.beginGroup(m_strSection);
	return setting.value(strKey, true).toBool();
}

void CEnviFilter::SaveEnviFilter()
{
	if (m_strSection.isEmpty())
		return;

	SaveCorp();
	SaveEnvi();
}

void CEnviFilter::SaveCorp()
{
	for (auto itr = m_mapBroker.begin();itr != m_mapBroker.end();++itr)
	{
		SetIniString(QString::number(itr->first),QString::number(itr->second));
	}
}

void CEnviFilter::SaveEnvi()
{
	if (m_bIsDispScheme)
		SetIniString("Disp","1");
	else
		SetIniString("Disp","0");

	SetIniString("CurID",m_strId);

	QString strTemp = QString::number(m_nFilterType);
	SetIniString("CurType",strTemp);

	if (m_bEnableChgDisp)
	{
		SetIniString("ChgDisp",QString::number(m_nChgDisp));
	}
}

QString CEnviFilter::GetIniString(const QString &strKey)
{
	QString path = FileSys::instance()->appCfgDir() + "/Filter.ini";
	QSettings setting(path, QSettings::IniFormat);

	setting.beginGroup(m_strSection);
	return setting.value(strKey, "").toString();
}

void CEnviFilter::SetIniString(const QString &strKey,const QString &strValue)
{
	QString path = FileSys::instance()->appCfgDir() + "/Filter.ini";
	QSettings setting(path, QSettings::IniFormat);

	setting.beginGroup(m_strSection);
	setting.setValue(strKey, strValue);
}


bool CEnviFilter::IsGroupExist(const QString &strGroupID)
{
	/* TODO
	CBondAttGroup& ag = AfxGetBondAttention().GetBondAttGroup();

	string sGroupID = SSW2A(strGroupID);
	std::list<AttGroupInfo>::iterator itr;
	for (itr=ag.m_group.begin();itr!=ag.m_group.end();++itr)
	{
		if (sGroupID.compare(itr->m_GroupId)==0)
			return true;
	}
	*/
	return false;
}

bool CEnviFilter::IsConditionExist(const QString &strGroupID)
{
	/* TODO
	CSSFilters* pFilter = AfxMangeProfile().GetSSFiltersCondition();
	if (pFilter==NULL)
		return false;

	BondsFilter* pbf = pFilter->GetFilterPtr(_ttoi(strGroupID));
	if (pbf==NULL)
		return false;
	else
		return true;
		*/
	return false;
}

QString CEnviFilter::AssembleID(int nType,const QString &strID)
{
	QString strTmp = QString::number(nType) + "|" + strID;

	return strTmp;
}

int CEnviFilter::SplitID(const QString &strSrv,QString& strID)
{
	if (strSrv.isEmpty())
		return FilterType::FilterType_NONE;

	int nTmp = strSrv.indexOf("|",0);
	QString strType = strSrv.mid(0,nTmp);
	nTmp++;
	strID = strSrv.mid(nTmp,strSrv.length()-nTmp);

	if (strType.isEmpty()||strID.isEmpty())
		return FilterType::FilterType_NONE;

	return strType.toInt();
}
